package com.btct;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends Activity {

	private static final String TAG = "doDiscovery";
	private static final boolean D = true;
	private BluetoothAdapter mBtAdapter;

	int pageno = 1;
	ArrayList<HashMap<String, String>> mylist;
	SimpleAdapter sa;
	ListView lv;
	View headView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		lv = (ListView) findViewById(R.id.ListView_Username);
		LayoutInflater infla = LayoutInflater.from(this);
		headView = infla.inflate(R.layout.listview_headitem, null);
		lv.addHeaderView(headView);
		mBtAdapter = BluetoothAdapter.getDefaultAdapter();

		mylist = new ArrayList<HashMap<String, String>>();
		doDiscovery();
		// lv.setVisibility(View.GONE);
		// Register for broadcasts when a device is discovered
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		this.registerReceiver(mReceiver, filter);

		// Register for broadcasts when discovery has finished
		filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		this.registerReceiver(mReceiver, filter);
		createSa();
		lv.setAdapter(sa);
		headView.setVisibility(View.GONE);
		
		headView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mylist.removeAll(mylist);
				doDiscovery();
			}
		});
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Main.this, Chat.class);
				if (position >= 1) {
					intent.putExtra("username",
							mylist.get(position - 1).get("ItemTitle"));
					startActivity(intent);
				}
			}
		});
	}

	private void doDiscovery() {
		if (D)
			Log.d(TAG, "doDiscovery()");

		// Indicate scanning in the title
		setProgressBarIndeterminateVisibility(true);
		// setTitle(R.string.scanning);

		// Turn on sub-title for new devices
		// findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);

		// If we're already discovering, stop it
		if (mBtAdapter.isDiscovering()) {
			mBtAdapter.cancelDiscovery();
		}

		// Request discover from BluetoothAdapter
		mBtAdapter.startDiscovery();
	}

	public void createSa() {
		sa = new SimpleAdapter(this, mylist, R.layout.listview_item_username,
				new String[] { "ItemTitle" }, new int[] { R.id.username });
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// Make sure we're not doing discovery anymore
		if (mBtAdapter != null) {
			mBtAdapter.cancelDiscovery();
		}

		// Unregister broadcast listeners
		this.unregisterReceiver(mReceiver);
	}
	

	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			headView.setVisibility(View.GONE);

			// When discovery finds a device
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				// Get the BluetoothDevice object from the Intent
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				// If it's already paired, skip it, because it's been listed
				// already
				if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("ItemTitle", device.getName());
					mylist.add(map);
				}
				createSa();
				lv.setAdapter(sa);

				// When discovery is finished, change the Activity title
			} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
					.equals(action)) {
				headView.setVisibility(View.VISIBLE);
				setProgressBarIndeterminateVisibility(false);
				if (sa.getCount() == 0) {
					Toast.makeText(context, "No device found.",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};
}
